{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 激活函数库定义\n",
    "由于tensorflow中未包括一些常用激活函数，因此这里统一定义，便于使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import argparse\n",
    "import sys\n",
    "import tensorflow as tf\n",
    "FLAGS = None\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ActivationFunction:\n",
    "#====================================================================\n",
    "# init\n",
    "    def __init__(self):\n",
    "    # 初始化\n",
    "        self.flag = 0\n",
    "    def elu(self,x,a=1):  \n",
    "        y=[]  \n",
    "        for i in x:  \n",
    "            if i>=0:  \n",
    "                y.append(i)  \n",
    "            else:  \n",
    "                y.append(a*np.exp(i)-1)  \n",
    "        return y\n",
    "    def selu(self,x):\n",
    "        with tf.name_scope('elu') as scope:\n",
    "            alpha = 1.6732632423543772848170429916717\n",
    "            scale = 1.0507009873554804934193349852946\n",
    "            return scale*tf.where(x>=0.0, x, alpha*tf.nn.elu(x))\n",
    "    def lrelu(self,x,alpha=0.2):\n",
    "        # tensorflow中的lrelu函数没有，改为自定义\n",
    "        # tf.nn.leaky_relu(features, alpha=0.2, name=None) \n",
    "        y=np.maximum(alpha*x,x) \n",
    "        return y\n",
    "    def prelu(self,x,alpha=0.1):\n",
    "        # 其中ai是可以学习的的。\n",
    "        # 如果ai=0，那么 PReLU 退化为ReLU；\n",
    "        # 如果 ai是一个很小的固定值（如ai=0.01），则 PReLU 退化为 Leaky ReLU（LReLU）。\n",
    "        y=np.maximum(alpha*x,x) \n",
    "        return y\n",
    "    def softplus(self,x):\n",
    "        y =np.log(np.exp(x)+1)  \n",
    "        return y\n",
    "    def softsign(self,x):\n",
    "        y=x/(np.abs(x)+1) \n",
    "        return y\n",
    "    def swish(self,x):\n",
    "        return x * tf.nn.sigmoid(x)\n",
    "    def sigmoid(self,x):\n",
    "        y=tf.nn.sigmoid(x)\n",
    "    #     y=1/(1+np.exp(-x))  #等效\n",
    "        return y\n",
    "    def tanh(self,x):\n",
    "        y=np.tanh(x)\n",
    "        return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#test\n",
    "#af = ActivationFunction()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
